package com.ebt.m.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.ebt.m.R;

/**
 * Created by Andy.Wang on 2016/4/8.
 */
public class CustomerRadioButton extends RadioButton implements CompoundButton.OnCheckedChangeListener {

    private static final int COLOR_DASH_SOLID_NORMAL = 0xffffffff;
    private static final int COLOR_DASH_SOLID_CHECKED = 0x33ffffff;
    private static final int COLOR_DASH_GAP_NORMAL =   0x00ffffff;
    private static final int COLOR_DASH_GAP_CHECKED =   0x00ffffff;

    private Paint mCirclePaint;
    private Paint mTextPaint;
    private int mHeight;
    private int mWidth;
    private String mTitle;
    private String mSubTitle;
    private int mNormalColor = Color.parseColor("#ffffff");
    private int mCheckedColor = Color.parseColor("#33ffffff");
    private int mPaddingBottom = 5;
    private int mScale;
    private float mDesSize;
    private float mNumberSize;
    private float mDescent;
    private float mCircleRadius;
    private float mStrokeWidth = 3;
    private boolean mDashCircle = false;
    private RectF mRectF;
    private int sweepUnitNum = 10;
    private int sweepAngleUnit = 360 / sweepUnitNum;
    private int sweepAngleSolid = sweepAngleUnit * 3 / 4;
    private int sweepAngleGap = sweepAngleUnit - sweepAngleSolid;

    public CustomerRadioButton(Context context) {
        this(context, null);
    }

    public CustomerRadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomerRadioButton, defStyleAttr, 0);
        mTitle = attributes.getString(R.styleable.CustomerRadioButton_number);
        mSubTitle = attributes.getString(R.styleable.CustomerRadioButton_des);
        mDesSize = attributes.getDimension(R.styleable.CustomerRadioButton_desSize, 30);
        mNumberSize = attributes.getDimension(R.styleable.CustomerRadioButton_numberSize, 30);
        mDashCircle = attributes.getBoolean(R.styleable.CustomerRadioButton_dashCircle, false);
        mScale = (int) getResources().getDisplayMetrics().density;
        mPaddingBottom *= mScale;
        attributes.recycle();
        initPaint();
        setWillNotDraw(false);
        setOnCheckedChangeListener(this);
        setClickable(true);
    }

    private void initPaint() {
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(Color.RED);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(mStrokeWidth);

        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(mDesSize);
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mDescent = Math.abs(fontMetrics.descent);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mCirclePaint.setColor(isChecked() ? mCheckedColor : mNormalColor);
        mTextPaint.setColor(isChecked() ? mCheckedColor : mNormalColor);
        if(mDashCircle){
            if(mRectF == null){
                mRectF = new RectF( mWidth / 2 - mCircleRadius,
                        mHeight / 2 - mCircleRadius,
                        mWidth / 2 + mCircleRadius,
                        mHeight / 2 + mCircleRadius);
            }
            drawDash(canvas, mRectF, mCirclePaint);
        }else{
            canvas.drawCircle(mWidth / 2, mHeight / 2, mCircleRadius, mCirclePaint);
        }
        mTextPaint.setTextSize(mNumberSize);
        if (!TextUtils.isEmpty(mTitle)) {
            canvas.drawText(mTitle, mWidth / 2, mHeight / 2, mTextPaint);
        }
        mTextPaint.setTextSize(mDesSize);
        if (!TextUtils.isEmpty(mSubTitle)) {
            canvas.drawText(mSubTitle, mWidth / 2, mHeight - mPaddingBottom - mDescent, mTextPaint);
        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        mCircleRadius = Math.min(w / 2, h / 2) - mStrokeWidth;
        invalidate();
    }

    public void setTitle(String title) {
        if (!mTitle.equalsIgnoreCase(title)) {
            mTitle = title;
            invalidate();
        }
    }

    public String getTitle() {
        return mTitle;
    }

    public void setSubTitle(String subTitle) {
        if (!mSubTitle.equalsIgnoreCase(subTitle)) {
            mSubTitle = subTitle;
            invalidate();
        }
    }

    public String getSubTitle() {
        return mSubTitle;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        invalidate();
    }

    //added by rick
    private void drawDash(Canvas canvas, RectF rectF, Paint paint){
        int colorSaved = paint.getColor();
        for(int i = 0; i < sweepUnitNum; i++) {
            paint.setColor(isChecked() ? COLOR_DASH_SOLID_CHECKED : COLOR_DASH_SOLID_NORMAL);
            canvas.drawArc(rectF, i * sweepAngleUnit, sweepAngleSolid, false, paint);
            paint.setColor(isChecked() ? COLOR_DASH_GAP_CHECKED : COLOR_DASH_GAP_NORMAL);
            canvas.drawArc(rectF, i * sweepAngleUnit + sweepAngleSolid, sweepAngleGap, false, paint);
        }
        paint.setColor(colorSaved);
    }

}
